package com.example.unitestexample.unittests.service;

import com.example.unitestexample.entity.Employee;
import com.example.unitestexample.repository.EmployeeRepository;
import com.example.unitestexample.service.EmployeeService;
import com.example.unitestexample.service.EmployeeServiceImplWithContructorInject;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class EmployeeServiceImplWithContructorInjectTest2 {

    @Mock
    private EmployeeRepository employeeRepository;
    private EmployeeService employeeService;

    @BeforeEach
    void setUp() {
        employeeService = new EmployeeServiceImplWithContructorInject(employeeRepository);
    }

    @Test
    public void whenValidName_thenEmployeeShouldBeFound() {
        String name = "alex";
        //setup the mocking
        when(employeeRepository.findByName(name)).thenReturn(Optional.of(new Employee(name)));

        // call the business method
        Optional<Employee> found = employeeService.getEmployeeByName(name);

        // assertions
        Assert.assertTrue(found.isPresent());
        Assert.assertEquals(found.get().getName(), name);
    }
}